<?php
if(!isset($_REQUEST['cid']) || !isset($_REQUEST['date'])) die('Invalid usage - missing param');
$cid = $_REQUEST['cid'];
$date = $_REQUEST['date'];

define('CID', $cid);
define('READ_ONLY_SESSION',true);
require_once('../../../include.php');
ModuleManager::load_modules();

$manager =  CRM_ContactsCommon::get_my_record();
$records = SCTV_MaintenanceCommon::get_maintenance_by_technical($date, null, $manager['id'], false);

if ($records === null) {
	die('Invalid usage - missing param');
}

set_time_limit(0);

header('Content-Encoding: UTF-8');
header('Content-type: text/csv; charset=UTF-8');
header('Content-disposition: attachement; filename="'. date('Y_m_d__H_i_s').'.csv"');
if (headers_sent())
	die('Some data has already been output to browser, can\'t send the file');
	$cols = array(
			__('Technical')=>array('width'=>25),
			__('Code')=>array('width'=>10),
			__('Customer Name')=>array('width'=>30),
			__('Address')=>array('width'=>10),
			__('Street')=>array('width'=>20),
			__('Area')=>array('width'=>15),
			__('Ward')=>array('width'=>15),
			__('District')=>array('width'=>15),				
			__('Phone')=>array('width'=>20),
			__('Note')=>array('width'=>30),
			__('Charge')=>array('width'=>30)
			);			

	require_once 'modules/Libs/PHPExcel/lib/PHPExcel.php';
	require_once 'modules/Libs/PHPExcel/lib/PHPExcel/Writer/Excel5.php';		
	$objPHPExcel = new PHPExcel();
	
	$row = 1;$column = 0;
	$sheet = $objPHPExcel->getActiveSheet();
		
	foreach ($cols as $key=>$value){
		$sheet->getColumnDimensionByColumn($column)->setWidth($value['width']);
			
		$sheet->setCellValueByColumnAndRow($column, $row, $key);
			
		$column++;
	}
	
	foreach ($records as $value) {
		$rec = array();
	
		$val = $value['technical_name'];
		$rec[] = SCTV_ReportsCommon::processing($val);
		$val = $value['requirement_code'];
		$rec[] = SCTV_ReportsCommon::processing($val);
		$val = $value['customer_name'];
		$rec[] = SCTV_ReportsCommon::processing($val);
		$val = $value['address'];
		$rec[] = SCTV_ReportsCommon::processing($val);
		$val = $value['street'];
		$rec[] = SCTV_ReportsCommon::processing($val);
		$val = $value['area'];
		$rec[] = SCTV_ReportsCommon::processing($val);
		$val = $value['ward'];
		$rec[] = SCTV_ReportsCommon::processing($val);
		$val = $value['district'];
		$rec[] = SCTV_ReportsCommon::processing($val);					
		$val = $value['phone'];
		$rec[] = SCTV_ReportsCommon::processing($val);
		$val = $value['note'];
		$rec[] = SCTV_ReportsCommon::processing($val);
		$val = $value['charge'];
		$rec[] = SCTV_ReportsCommon::processing($val);
						
		$row++;$column = 0;
		
		foreach ($rec as $value){			
			$sheet->setCellValueByColumnAndRow($column, $row, $value);			
			$column++;
		}
	}
	
	$objWriter = new PHPExcel_Writer_Excel5($objPHPExcel);
		
	header('Content-Encoding: UTF-8');
	header('Content-type: text/xls; charset=UTF-8');
	header('Content-disposition: attachement; filename="lapdat_' .date('Y_m_d__H_i_s').'.xls"');
	$objWriter->save('php://output');
?>